package com.example.controller;

import com.example.common.Result;
import com.example.pojo.dto.CartQueryDto;
import com.example.pojo.entity.Cart;
import com.example.pojo.vo.CartQueryVo;
import com.example.service.CartService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 购物车 controller 层
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    @PostMapping("/add")
    public Result add(@RequestBody Cart cart) {
        this.cartService.add(cart);
        return Result.success();
    }

    @GetMapping("/query")
    public Result query(CartQueryDto dto) {
        List<CartQueryVo> voList = this.cartService.query(dto);
        return Result.success(voList);
    }

    @PutMapping("/update/number/add")
    public Result addGoodsNumber(@RequestParam Integer id) {
        this.cartService.updateGoodsNumber(id, 1);
        return Result.success();
    }

    @PutMapping("/update/number/sub")
    public Result subGoodsNumber(@RequestParam Integer id) {
        this.cartService.updateGoodsNumber(id, -1);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        this.cartService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        this.cartService.deleteBatch(ids);
        return Result.success();
    }
}
